package p5;

/**
 * @author krystal
 * @date 2022/11/10 13:50
 */
public class Demo6 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        System.out.println(binarySearch(arr, 3));
    }

    public static int binarySearch(int[] arr,int toFind){
        int left = 0;
        int rirht = arr.length-1;
        while (left < rirht){
            int mid = (left + rirht) / 2;
            if (toFind < arr[mid]){
                //在左区间找
                rirht = mid - 1;
            }else if (toFind > arr[mid]){
                //在右区间找
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
